package com.yjyw.tiexie.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.yjyw.tiexie.R;
import com.yjyw.tiexie.bean.event.PayEvent;
import com.yjyw.tiexie.util.LogUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends Activity implements
        IWXAPIEventHandler {
    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //WEIXIN_APP_ID
        api = WXAPIFactory.createWXAPI(this, "");
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        LogUtil.d(TAG, "BaseReq, BaseReq = " + baseReq.openId);
    }

    @Override
    public void onResp(BaseResp resp) {
        LogUtil.d(TAG, "onPayFinish, errCode = " + resp.errCode);

        /**
         * int ERR_OK = 0;
         int ERR_COMM = -1;
         int ERR_USER_CANCEL = -2;
         int ERR_SENT_FAILED = -3;
         int ERR_AUTH_DENIED = -4;
         int ERR_UNSUPPORT = -5;
         */
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            String msg = "";
            if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
                msg = getString(R.string.pay_success);
            } else if (resp.errCode == BaseResp.ErrCode.ERR_COMM) {
//                msg = "一般错误，请重新尝试";
                msg = getString(R.string.label_pay_fail);
            } else if (resp.errCode == BaseResp.ErrCode.ERR_USER_CANCEL) {
                msg = getString(R.string.pay_cancel);
            } else if (resp.errCode == BaseResp.ErrCode.ERR_SENT_FAILED) {
//                msg = "发送失败，请重新尝试";
                msg = getString(R.string.label_pay_fail);
            } else if (resp.errCode == BaseResp.ErrCode.ERR_AUTH_DENIED) {
//                msg = "认证被否决，请重新尝试";
                msg = getString(R.string.label_pay_fail);
            } else if (resp.errCode == BaseResp.ErrCode.ERR_UNSUPPORT) {
//                msg = "不支持错误，请重新尝试";
                msg = getString(R.string.label_pay_fail);
            }
            LogUtil.d(TAG, "errorCode:" + resp.errCode + "  errorStr:" + resp.errStr + " msg:" + msg);

            PayEvent payEvent = new PayEvent();
            payEvent.setType(2);
            payEvent.setErrorMsg(msg);
            payEvent.setErrorCode(resp.errCode);
            EventBus.getDefault().post(payEvent);
            //通知支付成功 界面跳转交给界面去分发，不在回调里处理
            finish();
        }
    }
}
